package com.ftt.customer.dao;

import com.ftt.DBUtile;
import com.ftt.customer.model.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CustomerDao {
    //根据会员名称查询会员
    //改错1：传参
    public Customer getCustomerByName(String username ){
        Connection conn= DBUtile.getConnection();
        //改错2：sql语句
        String sql="select * from tb_customer where user_name=?";
        PreparedStatement ps =null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            rs= ps.executeQuery();
            if (rs.next()){
                Customer customer =new Customer();
                customer.setId(rs.getInt("id"));
                customer.setEmail(rs.getString("email"));
                customer.setMobile(rs.getString("mobile"));
                customer.setPassword(rs.getString("password"));
                customer.setReal_name(rs.getString("real_name"));
                //改错3：添加用户名
                customer.setUser_name( rs.getString("user_name"));
                customer.setPassword_question(rs.getString("password_question"));
                customer.setPassword_hint_answer(rs.getString("password_hint_answer"));
                return customer;
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            DBUtile.closeResource(conn,ps,rs);
        }
        return null;
    }
}
